export type Language = 'zh' | 'en';

export interface TranslationMap {
    [key: string]: { [lang in Language]: string };
}

export const translations: TranslationMap = {
    '温馨提醒': {
        'zh': '温馨提醒',
        'en': 'Tips'
    },
    '若有不适请立即退出地图': {
        'zh': '若有不适请立即退出地图',
        'en': 'Please exit the map immediately if you feel unwell'
    },
    '如果觉得暗，请在画面设置中调高伽马': {
        'zh': '如果觉得暗，请在画面设置中调高伽马',
        'en': 'If it feels dark, please increase gamma in the graphics settings'
    },
    '视角选择': {
        'zh': '视角选择',
        'en': 'Camera Mode'
    },
    '选项：': {
        'zh': '选项：',
        'en': 'Options:'
    },
    '你晕3D吗？': {
        'zh': '你晕3D吗？',
        'en': 'Do you feel dizzy with 3D?'
    },
    '晕': {
        'zh': '晕',
        'en': 'Yes'
    },
    '不晕': {
        'zh': '不晕',
        'en': 'No'
    },
    '特别鸣谢': {
        'zh': '特别鸣谢',
        'en': 'Special Thanks'
    },
    '@符华（283432408095765）的后室-研究人员（模型）\n@坦率的血翼蝠5801（12823830）的代码\n@EMO屑幽幽（@屑夜awa：50087305）的恐怖爷爷、恐怖奶奶、兰德里纳河（模型）\n@吉吉喵（455） 的搬运\n@搬砖喵（85487）的创作端修复\n@暗23（50480768）和@元元BU1e（13306211）的合作辅助\n以及其他，更多，如果你的信息没有记在上面，请在评论区申诉给我们': {
        'zh': '@符华（283432408095765）的后室-研究人员（模型）\n@坦率的血翼蝠5801（12823830）的代码\n@EMO屑幽幽（@屑夜awa：50087305）的恐怖爷爷、恐怖奶奶、兰德里纳河（模型）\n@吉吉喵（455） 的搬运\n@搬砖喵（85487）的创作端修复\n@暗23（50480768）和@元元BU1e（13306211）的合作辅助\n以及其他，更多，如果你的信息没有记在上面，请在评论区申诉给我们',
        'en': '@符华（283432408095765） for Backrooms - Researcher (model)\n@坦率的血翼蝠5801（12823830） for code\n@EMO屑幽幽（@屑夜awa：50087305） for Horror Grandpa, Horror Grandma, Landrina (models)\n@吉吉喵（455） for搬运\n@搬砖喵（85487） for creator side fixes\n@暗23（50480768） and @元元BU1e（13306211） for collaboration assistance\nAnd many others. If your information is not listed above, please appeal in the comment section.'
    },
    '第{day}天': {
        'zh': '第{day}天',
        'en': 'Day {day}'
    },
    '温馨提示': {
        'zh': '温馨提示',
        'en': 'Friendly Reminder'
    },
    '感谢您的游玩，期待您的再次光临！': {
        'zh': '感谢您的游玩，期待您的再次光临！',
        'en': 'Thank you for playing, we look forward to your next visit!'
    },
    '语言选择': {
        'zh': '语言选择',
        'en': 'Language Selection'
    },
    '请选择游戏语言': {
        'zh': '请选择游戏语言',
        'en': 'Please select game language'
    },
    '中文': {
        'zh': '中文',
        'en': 'Chinese'
    },
    'English': {
        'zh': 'English',
        'en': 'English'
    },
    '欢迎回来': {
        'zh': '欢迎回来',
        'en': 'Welcome back'
    },
    '{player}从梦魇回来辣': {
        'zh': '{player}从梦魇回来辣',
        'en': '{player} came back from the Nightmare!'
    },
    '菜单': {
        'zh': '菜单',
        'en': 'Menu'
    },
    '空气': {
        'zh': '空气',
        'en': 'nothing'
    },
    '开锁能力': {
        'zh': '开锁能力',
        'en': 'Lock picking ability'
    },
    '攻击性': {
        'zh': '攻击性',
        'en': 'Aggressiveness'
    },
    '你现在拿着{onHand}，你拥有的能力有↓': {
        'zh': '你现在拿着{onHand}，你拥有的能力有↓',
        'en': 'You are holding {onHand}, your abilities are:'
    },
    '鳄鱼生成失败': {
        'zh': '鳄鱼生成失败',
        'en': 'Crocodile spawn failed'
    },
    '你被鳄鱼袭击，剩余血量{hp}/{maxHp}': {
        'zh': '你被鳄鱼袭击，剩余血量{hp}/{maxHp}',
        'en': 'You were attacked by a crocodile, remaining HP: {hp}/{maxHp}'
    },
    '你被恐怖奶奶袭击，剩余血量{hp}/{maxHp}': {
        'zh': '你被恐怖奶奶袭击，剩余血量{hp}/{maxHp}',
        'en': 'You were attacked by Granny, remaining HP: {hp}/{maxHp}'
    },
    '你被机器人袭击，剩余血量{hp}/{maxHp}': {
        'zh': '你被机器人袭击，剩余血量{hp}/{maxHp}',
        'en': 'You were attacked by a robot, remaining HP: {hp}/{maxHp}'
    },
    '你被恐怖爷爷袭击，剩余血量{hp}/{maxHp}': {
        'zh': '你被恐怖爷爷袭击，剩余血量{hp}/{maxHp}',
        'en': 'You were attacked by Grandpa, remaining HP: {hp}/{maxHp}'
    },
    '你被兰德里纳河袭击，剩余血量{hp}/{maxHp}': {
        'zh': '你被兰德里纳河袭击，剩余血量{hp}/{maxHp}',
        'en': 'You were attacked by Slendrina, remaining HP: {hp}/{maxHp}'
    },
    '鳄鱼离开了1秒': {
        'zh': '鳄鱼离开了1秒',
        'en': 'Crocodile left for 1 second'
    },
    '鳄鱼回游！': {
        'zh': '鳄鱼回游！',
        'en': 'Crocodile returns!'
    },
    '欢迎来到不一样的恐怖奶奶3': {
        'zh': '欢迎来到不一样的恐怖奶奶3',
        'en': 'Welcome to Different Horror Granny 3'
    },
    '前往-噩梦模式': {
        'zh': '前往-噩梦模式',
        'en': 'Go to Nightmare Mode'
    },
    '我要玩单机游戏': {
        'zh': '我要玩单机游戏',
        'en': 'I want to play single-player game'
    },
    '抱歉！游客无法体验单机模式！': {
        'zh': '抱歉！游客无法体验单机模式！',
        'en': 'Sorry! Tourists cannot experience single-player mode!'
    },
    '单机版本': {
        'zh': '单机版本',
        'en': 'Single-player Version'
    },
    '我要玩单机版本，玩得清净！': {
        'zh': '我要玩单机版本，玩得清净！',
        'en': 'I want to play the single-player version, enjoy quietly!'
    },
    '简单模式': {
        'zh': '简单模式',
        'en': 'Easy Mode'
    },
    '正常模式': {
        'zh': '正常模式',
        'en': 'Normal Mode'
    },
    '困难模式(未上架)': {
        'zh': '困难模式(未上架)',
        'en': 'Hard Mode (Not Available)'
    },
    '极限模式(已下架)': {
        'zh': '极限模式(已下架)',
        'en': 'Extreme Mode (Removed)'
    },
    '梦魇模式': {
        'zh': '梦魇模式',
        'en': 'Nightmare Mode'
    },
    '开锁器': {
        'zh': '开锁器',
        'en': 'Lock Pick'
    },
    '你获得了开锁器，现在开始，大部分锁的门都可以直接进入了！': {
        'zh': '你获得了开锁器，现在开始，大部分锁的门都可以直接进入了！',
        'en': 'You got the lock pick! Now you can enter most locked doors directly!'
    },
    '方向盘': {
        'zh': '方向盘',
        'en': 'Steering Wheel'
    },
    '已获得方向盘×1，{onHand}放回原位': {
        'zh': '已获得方向盘×1，{onHand}放回原位',
        'en': 'Got steering wheel ×1, {onHand} returned to original position'
    },
    'OUCH！': {
        'zh': 'OUCH！',
        'en': 'OUCH！'
    },
    '木板': {
        'zh': '木板',
        'en': 'Wooden Board'
    },
    '成功拾起钥匙，目前手持钥匙{key}/3把': {
        'zh': '成功拾起钥匙，目前手持钥匙{key}/3把',
        'en': 'Successfully picked up the key, currently holding {key}/3 keys'
    },
    '放置成功！': {
        'zh': '放置成功！',
        'en': 'Placement successful!'
    },
    '我还需要一根玻璃保险丝': {
        'zh': '我还需要一根玻璃保险丝',
        'en': 'I still need a glass fuse'
    },
    '按钮': {
        'zh': '按钮',
        'en': 'Button'
    },
    '一个巨大的声音从远处传来……': {
        'zh': '一个巨大的声音从远处传来……',
        'en': 'A loud sound came from far away...'
    },
    '钥匙不足，无法奏效。当前钥匙{key}/3个': {
        'zh': '钥匙不足，无法奏效。当前钥匙{key}/3个',
        'en': 'Not enough keys, cannot work. Current keys: {key}/3'
    },
    '壁炉': {
        'zh': '壁炉',
        'en': 'Fireplace'
    },
    '呼噜！你试着生火，成功了！': {
        'zh': '呼噜！你试着生火，成功了！',
        'en': 'Whoosh! You tried to make a fire, and succeeded!'
    },
    '检查': {
        'zh': '检查',
        'en': 'Inspect'
    },
    '他身上什么也没有了，你已经夺去了他的唯一（霰弹枪×1）': {
        'zh': '他身上什么也没有了，你已经夺去了他的唯一（霰弹枪×1）',
        'en': 'There is nothing left on him. You have taken his only possession (Shotgun ×1)'
    },
    '你在他身上找到了一把霰弹枪！获得霰弹枪×1': {
        'zh': '你在他身上找到了一把霰弹枪！获得霰弹枪×1',
        'en': 'You found a shotgun on him! Got shotgun ×1'
    },
    '我还需要 方向盘×1': {
        'zh': '我还需要 方向盘×1',
        'en': 'I still need a steering wheel ×1'
    },
    '玻璃保险丝': {
        'zh': '玻璃保险丝',
        'en': 'Glass Fuse'
    },
    '已获得玻璃保险丝×1，{onHand}放回原位': {
        'zh': '已获得玻璃保险丝×1，{onHand}放回原位',
        'en': 'Got glass fuse ×1, {onHand} returned to original position'
    },
    '门上锁着……': {
        'zh': '门上锁着……',
        'en': 'The door is locked...'
    },
    '你还需要 木屋钥匙': {
        'zh': '你还需要 木屋钥匙',
        'en': 'You still need the cabin key'
    },
    '椰子': {
        'zh': '椰子',
        'en': 'Coconut'
    },
    '已获得椰子×1，{onHand}放回原位': {
        'zh': '已获得椰子×1，{onHand}放回原位',
        'en': 'Got coconut ×1, {onHand} returned to original position'
    },
    '断头台': {
        'zh': '断头台',
        'en': 'Guillotine'
    },
    '从中跌落泰迪熊×1，已拾取，{onHand}放回原位': {
        'zh': '从中跌落泰迪熊×1，已拾取，{onHand}放回原位',
        'en': 'A teddy bear ×1 fell out and was picked up, {onHand} returned to original position'
    },
    '这里还需要一个球形的东西': {
        'zh': '这里还需要一个球形的东西',
        'en': 'A spherical object is still needed here'
    },
    '兰德里纳河的摇篮': {
        'zh': '兰德里纳河的摇篮',
        'en': 'Landrina\'s Cradle'
    },
    '你把泰迪熊放入了摇篮，兰德里纳河似乎出现了……她不会再伤害你了……': {
        'zh': '你把泰迪熊放入了摇篮，兰德里纳河似乎出现了……她不会再伤害你了……',
        'en': 'You put the teddy bear into the cradle. Landrina seems to have appeared... she won\'t hurt you anymore...'
    },
    '这个摇篮温馨而恐怖……需要什么东西放在其中……': {
        'zh': '这个摇篮温馨而恐怖……需要什么东西放在其中……',
        'en': 'This cradle is warm yet terrifying... something is needed to be placed inside...'
    },
    '保险箱': {
        'zh': '保险箱',
        'en': 'Safe'
    },
    '已获得小硬币×1，{onHand}放回原位': {
        'zh': '已获得小硬币×1，{onHand}放回原位',
        'en': 'Got small coin ×1, {onHand} returned to original position'
    },
    '你还需要安全钥匙': {
        'zh': '你还需要安全钥匙',
        'en': 'You still need the security key'
    },
    '生锈的检票机器': {
        'zh': '生锈的检票机器',
        'en': 'Rusty ticket machine'
    },
    '你把硬币塞入，现在你可以乘坐地铁了！': {
        'zh': '你把硬币塞入，现在你可以乘坐地铁了！',
        'en': 'You inserted the coin, now you can take the subway!'
    },
    '这里需要塞个什么东西进去（投币口）': {
        'zh': '这里需要塞个什么东西进去（投币口）',
        'en': 'Something needs to be inserted here (coin slot)'
    },
    '废弃许久的油箱': {
        'zh': '废弃许久的油箱',
        'en': 'Long-abandoned fuel tank'
    },
    '地铁可以发动了，你已经充好油了！': {
        'zh': '地铁可以发动了，你已经充好油了！',
        'en': 'The subway can start, you have filled it with oil!'
    },
    '即将发动！5……': {
        'zh': '即将发动！5……',
        'en': 'Starting soon! 5...'
    },
    '即将发动！4……': {
        'zh': '即将发动！4……',
        'en': 'Starting soon! 4...'
    },
    '即将发动！3……': {
        'zh': '即将发动！3……',
        'en': 'Starting soon! 3...'
    },
    '即将发动！2……': {
        'zh': '即将发动！2……',
        'en': 'Starting soon! 2...'
    },
    '即将发动！1……': {
        'zh': '即将发动！1……',
        'en': 'Starting soon! 1...'
    },
    '传送失败：': {
        'zh': '传送失败：',
        'en': 'Teleport failed: '
    },
    '发动！': {
        'zh': '发动！',
        'en': 'Start!'
    },
    '你还需要汽油': {
        'zh': '你还需要汽油',
        'en': 'You still need gasoline'
    },
    '发霉的汽油': {
        'zh': '发霉的汽油',
        'en': 'Moldy gasoline'
    },
    '已获得汽油×1，{onHand}放回原位': {
        'zh': '已获得汽油×1，{onHand}放回原位',
        'en': 'Got gasoline ×1, {onHand} returned to original position'
    },
    '{player}逃离了这里！获得发光、飞行能力！': {
        'zh': '{player}逃离了这里！获得发光、飞行能力！',
        'en': '{player} escaped from here! Gained light emission and flight abilities!'
    },
    '{player}通过地铁逃离了这里！获得发光、飞行能力！': {
        'zh': '{player}通过地铁逃离了这里！获得发光、飞行能力！',
        'en': '{player} escaped via subway! Gained light emission and flight abilities!'
    },
    '汽车': {
        'zh': '汽车',
        'en': 'Car'
    },
    '已获得电缆×1，{onHand}放回原位': {
        'zh': '已获得电缆×1，{onHand}放回原位',
        'en': 'Got cable ×1, {onHand} returned to original position'
    },
    '齿轮排': {
        'zh': '齿轮排',
        'en': 'Gear Row'
    },
    '你获得了麻醉针，一发入魂！': {
        'zh': '你获得了麻醉针，一发入魂！',
        'en': 'You got an anesthetic needle, one shot to knock out!'
    },
    '我还需要一根电缆': {
        'zh': '我还需要一根电缆',
        'en': 'I still need a cable'
    },
    '盒子': {
        'zh': '盒子',
        'en': 'Box'
    },
    '你获得了弹弓！': {
        'zh': '你获得了弹弓！',
        'en': 'You got a slingshot!'
    },
    '我还需要武器钥匙': {
        'zh': '我还需要武器钥匙',
        'en': 'I still need the weapon key'
    },
    '前往正常模式': {
        'zh': '前往正常模式',
        'en': 'Go to Normal Mode'
    },
    '{player}，你想尝试挑战这个普通模式吗？': {
        'zh': '{player}，你想尝试挑战这个普通模式吗？',
        'en': '{player}, do you want to try challenging this normal mode?'
    },
    '是': {
        'zh': '是',
        'en': 'Yes'
    },
    '否': {
        'zh': '否',
        'en': 'No'
    },
    '分享地图': {
        'zh': '分享地图',
        'en': 'Share Map'
    },
    '分享': {
        'zh': '分享',
        'en': 'Share'
    },
    '走三楼木板': {
        'zh': '走三楼木板',
        'en': 'Walk on third floor planks'
    },
    '走桥': {
        'zh': '走桥',
        'en': 'Walk on bridges'
    },
    '生火成功！': {
        'zh': '生火成功！',
        'en': 'Fire started successfully!'
    },
    '逃离能力': {
        'zh': '逃离能力',
        'en': 'Escape ability'
    },
    '兰德里纳河不再打': {
        'zh': '兰德里纳河不再打',
        'en': 'Ladrinn River won\'t attack you'
    },
    '投币成功': {
        'zh': '投币成功',
        'en': 'Coin inserted successfully'
    },
    '不被敌人看见': {
        'zh': '不被敌人看见',
        'en': 'Invisible to enemies'
    },
    '进入木屋': {
        'zh': '进入木屋',
        'en': 'Enter wooden house'
    },
    '安全钥匙': {
        'zh': '安全钥匙',
        'en': 'Safety key'
    },
    '开启汽车后备箱': {
        'zh': '开启汽车后备箱',
        'en': 'Open car trunk'
    },
    '我': {
        'zh': '我',
        'en': 'I'
    },
    '我必须离开这间屋子': {
        'zh': '我必须离开这间屋子',
        'en': 'I must leave this house'
    },
    '你获得了木屋钥匙，现在开始，你可以进入木屋了': {
        'zh': '你获得了木屋钥匙，现在开始，你可以进入木屋了',
        'en': 'You got the cabin key! Now you can enter the wooden house!'
    },
    '成功拾起安全钥匙': {
        'zh': '成功拾起安全钥匙',
        'en': 'Successfully picked up the safety key'
    },
    '你获得了汽车钥匙': {
        'zh': '你获得了汽车钥匙',
        'en': 'You got the car key'
    },
    '你获得了武器钥匙，{onHand}放回原位': {
        'zh': '你获得了武器钥匙，{onHand}放回原位',
        'en': 'You got the weapon key, {onHand} returned to original position'
    },
    'description': {
        'zh': 'description',
        'en': 'description'
    },
    '你用手上的方向盘吸附在了上面，旋转了！': {
        'zh': '你用手上的方向盘吸附在了上面，旋转了！',
        'en': 'You used the steering wheel in your hand to attach to it and rotate!'
    },
    '让这一份惊恐分享给更多人': {
        'zh': '让这一份惊恐分享给更多人',
        'en': 'Let this horror be shared with more people'
    }
};

export function translate(key: string, lang: Language, params?: Record<string, string | number>): string {
    const translation = translations[key];
    if (!translation) {
        return key; // 如果没有找到翻译，返回原文本
    }

    let result = translation[lang];

    // 处理参数替换
    if (params) {
        Object.keys(params).forEach(paramKey => {
            const regex = new RegExp(`\\{${paramKey}\\}`, 'g');
            result = result.replace(regex, String(params[paramKey]));
        });
    }

    return result;
}

export async function setupLanguage(entity: any): Promise<Language> {
    // 默认使用中文
    entity.language = 'zh';

    // 让玩家选择语言
    const langChoice = await entity.player.dialog({
        type: GameDialogType.SELECT,
        title: '语言选择\nLanguage Selection',
        content: '请选择游戏语言\nPlease select the game language',
        options: ['中文', 'English']
    });

    if (langChoice) {
        entity.language = langChoice.value === '中文' ? 'zh' : 'en';
    }

    return entity.language;
}